package br.com.informatos.testejson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Main extends  ListActivity{


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button btn_carregar = (Button) findViewById(R.id.btn_carregar);
		
		btn_carregar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayAdapter<Produto> adapter = new ArrayAdapter<Produto>(Main.this, android.R.layout.simple_list_item_1, listaDeProdutos());
				setListAdapter(adapter);
				
				
				
			}
		});


		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);


		
	}






		private List<Produto> listaDeProdutos(){
			List<Produto> produtos = new ArrayList<Produto>();
			String listaJSONString = pegaLista();

			if(listaJSONString !=null){
				try{
					JSONArray jsonSarray = new JSONArray(listaJSONString);

					for(int i=0; i<jsonSarray.length(); i++){
						JSONObject produtoJSON = jsonSarray.getJSONObject(i);

						Produto produto = new Produto(produtoJSON);
						produtos.add(produto);
					}
				}catch(JSONException e){
					e.printStackTrace();
				}
			}

			return produtos;

		}


		private String pegaLista(){
			
			try{
				URL url = new URL("http://192.168.1.102/k41/produtos");
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				InputStream inputStream = connection.getInputStream();
				
				String resposta = readStream(inputStream);
				
				return resposta;
			
			}catch (IOException e) {
				e.printStackTrace();
			}

			return null;

		}
		
		
		private String readStream(InputStream in){
			BufferedReader reader = null;
			StringBuilder builder = new StringBuilder();

			try{
				reader =new  BufferedReader(new InputStreamReader(in));
				String line = null;
				while((line = reader.readLine()) !=null){
					builder.append(line + "\n");
				}			

			}catch(IOException e){
				e.printStackTrace();
			}finally{
				if(reader !=null){
					try{
						reader.close();
					}catch(IOException e){
						e.printStackTrace();
					}
				}
			}
			return builder.toString();
		}


		
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			getMenuInflater().inflate(R.menu.menu, menu);
			return true;
		}

		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			if (item.getItemId() == R.id.add_Prod) {
				Intent intent = new Intent(this, NovoProduto.class);
				startActivity(intent);
			}
			return super.onOptionsItemSelected(item);
		}

		

		


	}


